package com.elvin.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.elvin.platform.beans.ConstantsPath;
import com.elvin.platform.beans.ResultState;
import com.elvin.platform.beans.TemplateMsgResultDto;
import com.elvin.platform.beans.WxTemplateMsg;
import com.elvin.platform.util.HttpUtil;
import com.elvin.platform.util.JsonUtils;
import lombok.extern.log4j.Log4j2;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
@Log4j2
public class SendWxTemplateMsg {
/*
    public TemplateMsgResultDto noticeTemplate() {
        // 模版ID
        String templateId="MOmXIZF_Gi0HYfExnliSuzRJI-YmK7Fp9-E6gmAr3VU";
        TreeMap<String, TreeMap<String, String>> params = new TreeMap<>();
        //根据具体模板参数组装
        params.put("first", WxTemplateMsg.item("恭喜！您的需求已发布成功", "#000000"));
        params.put("keyword1", WxTemplateMsg.item("", "#000000"));
        params.put("keyword2", WxTemplateMsg.item("需求已发布", "#000000"));
        params.put("remark", WxTemplateMsg.item("请耐心等待审核", "#000000"));
        WxTemplateMsg wxTemplateMsg = new WxTemplateMsg();
        // 模版ID
        wxTemplateMsg.setTemplate_id(templateId);
        // openId
        wxTemplateMsg.setTouser("ow4Sb5T1KmUtCNykgp5_rTA76PNk");
        // 关键字赋值
        wxTemplateMsg.setData(params);
        String data = JsonUtils.ObjectToString(wxTemplateMsg);
        return handleSendMsgLog(data);
    }
*/
    private TemplateMsgResultDto handleSendMsgLog(String data) {
        TemplateMsgResultDto resultDto = new TemplateMsgResultDto();
        try {
            sendTemplateMsg(data);
        } catch (Exception exception) {
            log.error("发送模版失败",  exception);
        }
        // TODO 可以记录一下发送记录的日志
        return resultDto;
    }


    public Object sendTemplateMsg(String data) throws Exception {
        // 获取token
        log.info("开始推送微信公众号消息，data:"+data);
        String accessToken = getAccessToken();
        // 发送消息
        JSONObject json = JSONObject.parseObject(HttpUtil.sendPost(ConstantsPath.SEND_MESSAGE_TEMPLATE_URL+ accessToken, data));
        log.info("微信公众号推送结果：", json.toJSONString());
        ResultState object = JsonUtils.getObject(json.toString(), ResultState.class);
        return object;
    }


    /**
     * 获取全局token
     */
    public String getAccessToken() {
        Map<String, String> requestUrlParam = new HashMap<>();
        requestUrlParam.put("appid", ConstantsPath.MINIAPI); // 小程序ID
        requestUrlParam.put("secret", ConstantsPath.SECRET); // 开发者设置中的appSecret
        requestUrlParam.put("grant_type", ConstantsPath.GRANT_TYPE); // 默认参数
        JSONObject json = JSONObject.parseObject(HttpUtil.sendPost(ConstantsPath.WX_ACCESS_TOKEN_URL, requestUrlParam));
        log.debug("getAccessToken json = {}", json);
        String access_token = json.getString("access_token");
        int expires_in = (Integer.parseInt(json.getString("expires_in"))) ;
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("access_token", access_token);
        resultMap.put("expires_in", expires_in);
        return  access_token;
    }

}
